package com.huika.dealontop.utils;

import android.content.SharedPreferences;

import com.huika.dealontop.GlobalApp;


/**
 * 数据储存 XML文件
 */
public class PreferencesHelper {

    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;

    private static PreferencesHelper mPreferencesHelper;
    private static final String NAME = "DealOnTop_preference";

    /**
     * 手动登录后最多维持14天自动登录有效期的规则
     */
    private static final String LOG_IN_EFFECTIVE_TIME = "log_in_effective_time";

    private static final String IMAGE_URI = "image_uri";// 存储登录界面的图像

    /**
     * 版本更新
     */
    public static final String LAST_VERSION_CHECK_TIME = "lastVersionCheckTime";
    public static final String VERSION_CODE = "currentVersionCode";// 当前版本号
    public static final String LAST_CHECK_VERSION = "lastCheckVersion";// 最新版本号

    /*Login保存账号密码*/
    public static final String KEY_LOGIN_NAME = "key_login_name";
    public static final String KEY_LOGIN_PWD = "key_login_pwd";
    public static final String KEY_LOGINISTHRID_NICKNAME = "key_login_isthird_nickname";
    public static final String KEY_LOGINISTHRID_USERID = "key_login_isthird_userId";
    public static final String KEY_ENVIRONMENT = "key_environment";
    /**
     * 定位的经纬度
     */
    public static final String CURRENT_LOCATION_LAT = "current_location_lat";
    public static final String CURRENT_LOCATION_LNG = "current_location_lng";


    private PreferencesHelper() {
        sp = GlobalApp.getInstance().getSharedPreferences(NAME, 0);
        editor = sp.edit();
    }


    synchronized public static PreferencesHelper getInstance() {

        if (null == mPreferencesHelper) {
            mPreferencesHelper = new PreferencesHelper();
        }
        return mPreferencesHelper;
    }

    /**
     * 储存值
     *
     * @param key
     * @param value
     */
    public static void setString(String key, String value) {
        editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static void setInt(String key, int value) {
        editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static void setLong(String key, long value) {
        editor = sp.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public static void setBoolean(String key, boolean value) {
        editor = sp.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 获取值
     *
     * @param key
     * @return
     */
    public static String getString(String key) {
        return sp.getString(key, "");
    }

    public static int getInt(String key) {
        return sp.getInt(key, -1);
    }

    public static long getLong(String key) {
        return sp.getLong(key, 0L);
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        return sp.getBoolean(key, defaultValue);
    }

    public  void remove(String name) {
        editor.remove(name);
        editor.commit();
    }


    public static void setImageUri(String imageUri) {
        editor.putString(IMAGE_URI, imageUri);
        editor.commit();
    }

    public static String getImageUri() {
        return sp.getString(IMAGE_URI, "");
    }

}
